package com.tdogg.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tdogg.entity.Cart;
import com.tdogg.entity.Users;
import com.tdogg.mapper.CartMapper;
import com.tdogg.service.ICartService;
import com.tdogg.util.RedisUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 *  购物车的实现类
 * 
 * @author Julie
 *
 */
@Service
public class CartServiceImpl implements ICartService {
	@Autowired
	private CartMapper cartMapper;

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) 
	/**
	 * 添加商品到购物车，返回结果集
	 */
	@Override
	public Integer addGoodsToCart(Cart cart) {
		Integer rs = cartMapper.addCart(cart);
		return rs;
	}
	/**
	 *  通过userid查找购物车，并返回购物车的商品id
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true) 
	@Override
	public Cart findCartByUserId(Integer userId, Integer goodsId) {
		return cartMapper.findCartByUserId(userId, goodsId);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) 
	@Override
	public Integer updateCart(Cart cart) {
		return cartMapper.updateCart(cart);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	@Override
	/**
	 *  通过userid查找购物车，并返回购物车的列表
	 */
	public List<Cart> findCartByUserId(Integer userId) {
		return cartMapper.findCartListByUserId(userId);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public Integer deleteCart(Integer cartId) {
		return cartMapper.deleteCart(cartId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	@Override
	public Cart findCartById(Integer cartId) {
		return cartMapper.findCartById(cartId);
	}

}
